package servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import actionForm.CreatePhoneValidationForm;
import domain.IDAOPhoneNumber;

public class ModifyPhoneAction extends Action {
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest request,
			final HttpServletResponse response) {
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServlet().getServletContext());
		IDAOPhoneNumber daoPhone = (IDAOPhoneNumber) context.getBean("daophonenumber");	
		CreatePhoneValidationForm form = (CreatePhoneValidationForm) pForm; 
		if (request.getMethod().equals("POST")) {
			String phoneId = form.getPhoneId();
			String contactId = form.getContactId();
				daoPhone.modifyPhoneNumber(Long.valueOf(phoneId) ,  request.getParameter("phoneKind"), request.getParameter("phoneNumber"));
				request.setAttribute("id", contactId);
				return new ActionRedirect(pMapping.findForward("modifyContact")).addParameter("id", contactId); 
		}
		else if (request.getMethod().equals("GET")){
			String phoneId = request.getParameter("phoneId");
			request.setAttribute("contactId", request.getParameter("contactId"));
			if (phoneId != null) {
				request.setAttribute("phone", daoPhone.getPhoneNumber(Long.valueOf(phoneId)));
				return pMapping.findForward("modifyPhoneNumber");
			}
			
		}
		return null;
	}
}